import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_juwo/common/common_index.dart';
import 'package:flutter_juwo/ui/custom/noData.dart';
import 'package:flutter_juwo/ui/pages/photoHero/PhotoHeroNet.dart';
import 'package:flutter_juwo/ui/pages/photoHero/showBigImageNet.dart';
import 'package:flutter_juwo/ui/utils/toast_utils.dart';

import 'model/detail_entity.dart';

class RepresentationDetails extends StatefulWidget {
  final String id;

  RepresentationDetails(this.id);

  @override
  _RepresentationDetailsState createState() => _RepresentationDetailsState();
}

class _RepresentationDetailsState extends State<RepresentationDetails> {
  MyPageState ps = MyPageState.LOADING;
  DetailEntity entity = null;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print("获取到id：${widget.id}");
    this.initData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: UiUtils.appBar(context, "申诉详情"),
      body: this.getContent(),
    );
  }

  void initData() {
    getData();
  }

  Future getData() async {
    FormData formData = FormData.from({"id": widget.id});
    DioManager.getInstance().get(Api.USER_GETAPPLY, formData, (json) {
      print("获取详情：${json}");
      entity = DetailEntity.fromJson(json);
      if (null == entity) {
        ///
        ps = MyPageState.NOT_DATA;
        setState(() {});
        return;
      }
      if (entity.code == Api.ERROR_CODE) {
        ToastUtil.showShortToast(entity.message);
        return;
      }

      if (entity.code == Api.SUCCESS_CODE) {
        ///
        if (null == entity.data) {
          ps = MyPageState.NOT_DATA;
          setState(() {});
          return;
        }

        ps = MyPageState.HAVE_DATA;
        setState(() {});
        return;
      }
    }, (errMsg) {
      print("获取详情ERR：${errMsg}");
    });
  }

  getContent() {
    if (ps == MyPageState.LOADING) {
      return LoadingPage();
    }

    if (ps == MyPageState.NOT_DATA) {
      return NoData(this.getData);
    }

    if (ps == MyPageState.HAVE_DATA) {
      return Container(
        child: EasyRefresh(
            child: ListView(
          children: <Widget>[
            this.getRow1(),
            this.getRow2(),
            this.getRow3(),
            this.getRow4(),
            this.getRow5(),
          ],
        )),
      );
    }
  }

  getRow1() {
    return Container(
      margin: EdgeInsets.only(top: ScreenUtil().setWidth(2)),
      color: Colors.white,
      padding: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(30),
          ScreenUtil().setWidth(30),
          ScreenUtil().setWidth(30),
          ScreenUtil().setWidth(30)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Container(
            child: Text(
              "申诉类型",
              style: TextStyles.custom(
                  30, Colours.appC2, Font.PingFangMedium),
            ),
          ),
          Container(
            child: Text(
              "${entity.data.problemName}",
              style: TextStyles.custom(
                  30, Colours.appC2, Font.PingFangMedium),
            ),
          ),
        ],
      ),
    );
  }

  getRow2() {
    return Container(
      margin: EdgeInsets.only(top: ScreenUtil().setWidth(2)),
      color: Colors.white,
      padding: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(30),
          ScreenUtil().setWidth(30),
          ScreenUtil().setWidth(30),
          ScreenUtil().setWidth(30)),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            child: Text(
              "申诉内容",
              style: TextStyles.custom(
                  30, Colours.appC2, Font.PingFangMedium),
            ),
          ),
          Container(
            width: ScreenUtil().setWidth(750),
            margin: EdgeInsets.only(top: ScreenUtil().setWidth(30)),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(2), color: Colours.c243),
            child: Padding(
              padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
              child: Text(
                "${entity.data.problem}",
                style: TextStyles.custom(
                    30, Colours.appC2, Font.PingFangMedium),
              ),
            ),
          ),
        ],
      ),
    );
  }

  getRow3() {
    List<Widget> list = [];
    for (int i = 0; i < entity.data.images.length; i++) {
      list.add(Padding(
        padding: EdgeInsets.only(right: ScreenUtil().setWidth(10)),
        child: UiUtils.cachedNetworkImage(200, 180, entity.data.images[i].img),
      ));
    }

    return Container(
      margin: EdgeInsets.only(
        top: ScreenUtil().setWidth(2),
      ),
      color: Colors.white,
      padding: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(30),
          ScreenUtil().setWidth(30),
          ScreenUtil().setWidth(30),
          ScreenUtil().setWidth(30)),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            child: Text(
              "申诉截图",
              style: TextStyles.custom(
                  30, Colours.appC2, Font.PingFangMedium),
            ),
          ),
          Container(
            height: ScreenUtil().setWidth(210),
            margin: EdgeInsets.only(top: ScreenUtil().setWidth(30)),
            child: ListView.builder(
                scrollDirection: Axis.horizontal,
                shrinkWrap: true,
                itemCount: entity.data.images.length,
                itemBuilder: (BuildContext context, int index) {
                  return Padding(
                    padding: EdgeInsets.only(right: ScreenUtil().setWidth(10)),
                    child: PhotoHeroNet(
                        photo: entity.data.images[index].img,
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => ShowBigImagePageNet(
                                    entity.data.images[index].img)),
                          );
                        },
                        width: ScreenUtil().setWidth(200))

                    /*UiUtils.cachedNetworkImage(
                        200, 180, entity.data.images[index].img)*/
                    ,
                  );
                }),
          ),
        ],
      ),
    );
  }

  getRow4() {
    return Container(
      margin: EdgeInsets.only(
        top: ScreenUtil().setWidth(2),
      ),
      color: Colors.white,
      padding: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(30),
          ScreenUtil().setWidth(30),
          ScreenUtil().setWidth(30),
          ScreenUtil().setWidth(30)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Container(
            child: Text(
              "申诉结果",
              style: TextStyles.custom(
                  30, Colours.appC2, Font.PingFangMedium),
            ),
          ),
          Container(
            child: Text(
              "${entity.data.status == "0" ? "待回复" : "已回复"}",
              style: TextStyles.custom(
                  32,
                  entity.data.status == "0" ? Colours.appC11 : Colors.green,
                  Font.PingFangMedium),
            ),
          ),
        ],
      ),
    );
  }

  getRow5() {
    return Container(
      padding: EdgeInsets.all(ScreenUtil().setWidth(30)),
      color: Colors.white,
      child: Container(
        padding: EdgeInsets.all(ScreenUtil().setWidth(20)),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(6), color: Colours.c243),
        child: Html(
          data: "${entity.data.reply}",
          blockSpacing: 0,
        ),
      ),
    );
  }
}
